﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DelegateCommand.cs" company="">
//   
// </copyright>
//  <summary>
//   The delegate command.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Shatranj
{
    using System;
    using System.Windows.Input;

    /// <summary>
    /// The delegate command.
    /// </summary>
    public class DelegateCommand : ICommand
    {
        #region Fields

        /// <summary>
        /// The can execute.
        /// </summary>
        private readonly Func<object, bool> canExecute;

        /// <summary>
        /// The can execute event handler.
        /// </summary>
        private readonly EventHandler canExecuteEventHandler;

        /// <summary>
        /// The execute.
        /// </summary>
        private readonly Action<object> execute;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DelegateCommand"/> class.
        /// </summary>
        /// <param name="canExecute">
        /// The can execute.
        /// </param>
        /// <param name="execute">
        /// The execute.
        /// </param>
        public DelegateCommand(bool canExecute, Action<object> execute)
            : this(x => canExecute, execute)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DelegateCommand"/> class.
        /// </summary>
        /// <param name="canExecute">
        /// The can execute.
        /// </param>
        /// <param name="execute">
        /// The execute.
        /// </param>
        public DelegateCommand(Func<object, bool> canExecute, Action<object> execute)
        {
            this.canExecute = canExecute;
            this.execute = execute;
        }

        #endregion

        #region Public Events

        /// <summary>
        /// The can execute changed.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The can execute.
        /// </summary>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool CanExecute(object parameter)
        {
            return this.canExecute(parameter);
        }

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        public void Execute(object parameter)
        {
            this.execute(parameter);
        }

        #endregion
    }
}